﻿using King.Box.AdminWeb.Utils;
using King.Box.Models.Db.Sys;
using King.Box.Models.Query.Sys;
using King.Box.Models.Util;
using King.Box.Models.View.Sys;
using King.Box.Utils.Global;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace King.Box.AdminWeb.Logic
{
    public class SysService
    {
        #region 菜单
        public ResponseData<PageResult<Sys_MenuData>> SelectMenu(SysMenuQuery query)
        {
            var data = ApiClientUtil.Get<PageResult<Sys_MenuData>>("sys/menu/getpage", query);
            return ResponseData<PageResult<Sys_MenuData>>.Success(data);
        }

        public ResponseData<List<Sys_MenuData>> SelectMenuLevel()
        {
            var data = ApiClientUtil.Get<List<Sys_MenuData>>("sys/menu/getlevel");
            return ResponseData<List<Sys_MenuData>>.Success(data);
        }

        public Sys_Menu GetMenu(int id)
        {
            return ApiClientUtil.Get<Sys_Menu>($"sys/menu/{id}");
        }
        
        public List<Sys_MenuData> SelectMenu()
        {
            return ApiClientUtil.Get<List<Sys_MenuData>>("sys/menu/getall");
        }

        public ResponseData SaveMenu(Sys_Menu menu)
        {
            bool result = false;

            {
                if (string.IsNullOrEmpty(menu.Name))
                    throw new ArgumentNullException(nameof(menu.Name));

                if (menu.Level == (int)GlobalConst.PleaseSelectValue)
                    throw new ArgumentNullException(nameof(menu.Level));

                if (menu.Level > (int)GlobalEnum.MenuLevel.Level1)
                {
                    if (string.IsNullOrEmpty(menu.Url))
                        throw new ArgumentNullException(nameof(menu.Url));
                    if (menu.ParentId < 0)
                        throw new ArgumentNullException(nameof(menu.ParentId));
                }
            }

            if (menu.Id > 0)
            {
                menu.UpdateTime = DateTime.Now;
                result = ApiClientUtil.Post<bool>("sys/menu/update", menu);
            }
            else
            {
                menu.IsDelete = (int)GlobalEnum.YN.No;
                menu.CreateTime = DateTime.Now;
                menu.UpdateTime = DateTime.Now;

                result = ApiClientUtil.Post<bool>("sys/menu/add", menu);
            }

            return result ? ResponseData.Success() : ResponseData.Failtrue(string.Empty);
        }

        public List<Sys_MenuData> SelectCanParentMenu()
        {
            var parentMenus = new List<Sys_MenuData>();

            var menuList = this.SelectMenu();
            menuList.ForEach(x =>
            {
                if (x.Level < (int)GlobalEnum.MenuLevel.Level3)
                    parentMenus.Add(x);
            });
            return parentMenus;
        }
        #endregion

        #region 权限
        public ResponseData<PageResult<Sys_PermissionData>> SelectSysPermissionDetail(SysPermissionQuery query)
        {
            var data = ApiClientUtil.Get<PageResult<Sys_PermissionData>>("sys/permission/getpage", query);
            data.rows.ToList().ForEach(x =>
            {
                switch (x.ReferType)
                {
                    case (int)GlobalEnum.SysPermissionReferType.Menu:
                        x.Refer = JsonConvert.DeserializeObject<Sys_Menu>(JsonConvert.SerializeObject(x.Refer));
                        break;
                    case (int)GlobalEnum.SysPermissionReferType.Operation:
                        x.Refer = JsonConvert.DeserializeObject<Sys_Permission>(JsonConvert.SerializeObject(x.Refer));
                        break;
                    default:
                        break;
                }
            });

            return ResponseData<PageResult<Sys_PermissionData>>.Success(data);
        }
        #endregion

        #region 用户
        public ResponseData<PageResult<Sys_UserData>> SelectUser(SysUserQuery query)
        {
            var data = ApiClientUtil.Get<PageResult<Sys_UserData>>("sys/user/getpage");
            return ResponseData<PageResult<Sys_UserData>>.Success(data);
        }
        #endregion
    }
}